<?php
namespace app\agent\controller;
use app\agent\controller\BaseController;

class ShopController  extends BaseController {
	
	public function goods($goods_sn="",$goods_name="",$trade_id=0) {
		if(!empty($goods_sn))$where['a.goods_sn'] = ['like','%'.$goods_sn.'%'];
		$this->assign('goods_sn',$goods_sn);
		if(!empty($goods_name))$where['a.goods_name'] = ['like','%'.$goods_name.'%'];
		$this->assign('goods_name',$goods_name);
		if(input('trade_id')){
			$where['a.trade_id'] = $trade_id;	
		}
		$this->assign('trade_id',$trade_id);
    	
    	$where['a.agent_id'] = session('agent_id');
		$where['a.is_on_sale'] = ['<>',3];
    	//获取当前店铺商品
		$goods_list = db('goods')
						->alias('a')
						->field('a.goods_id,a.cost_price,a.goods_sn,a.goods_name,a.sale_price,a.is_on_sale,a.is_recommend,a.add_time,a.stock,a.unit,a.trade_id,b.name')
						->join('good_cate b','a.trade_id = b.id','left')
						->where($where)
						->order('a.add_time desc')
						->paginate(15);
    	
    	$this->assign('goods_list',$goods_list);
    	
		//获取商品分类类型
		$goods_type = db('good_cate')
						->where('agent_id',session('agent_id'))
						->select();
		$this->assign('goods_type',$goods_type);
		$this->assign('empty','<span class="empty" >没有数据</span>');
    	
    	return $this->fetch();
	}
	
	function goods_add(){
		//获取商家分类
		
		$agent_type = db('good_cate')->select();
		$this->assign('agent_type',$agent_type);
		if(request()->isPost()){
			$data = $this->request->post();
			unset($data['file']);
			
			$data['agent_id'] = session('agent_id');
			$data['add_time'] = now_time();
			$result = add_data('goods',$data);
			
			
			if ($result) {
				$this->success('添加成功');
			} else {
				$this->error('添加失败');
			}
		}
		
		return $this->fetch();
	}
	
	//修改商品
    public function goods_edit(){
    	
		if(request()->isPost()){
			$param=input('post.');
			unset($param['file']);
			$res = db('goods')->update($param);
			if($res){
				$this->success('修改成功');
			}else{
				$this->success('修改失败');
			}
			
		}
    	$goods_id = input('goods_id');
		$goods_info = db('goods')
						->alias('a')
						->join('good_cate b','a.trade_id = b.id','left')
						->where('a.goods_id',$goods_id)
						->find();
		if($goods_info){
			//商品信息
			$goods_info['content'] = html_entity_decode($goods_info['content']);
			$goods_info['goods_id'] = $goods_id;
			$this->assign('goods_info',$goods_info);
			
//			//获取平台分类
//			$platform_type = db('goodstype')->select();
//			//1级分类
//			$platform_1 = array();
//			//2级分类
//			$platform_2 = array();
//			foreach($platform_type as $key=>$values){
//				if($values['pid'] == 0){
//					$platform_1[] = $values;
//				}else{
//					$platform_2[$values['pid']][] = $values;
//				}
//			}
//			$this->assign('platform_1',$platform_1);
//			$this->assign('platform_2',$platform_2);
			//获取商家分类
			$agent_type = db('good_cate')->where('agent_id',session('agent_id'))->select();
			
			$this->assign('agent_type',$agent_type);
			
		}else{
			$this->error('商品不存在');
		}
		
		return $this->fetch();
    }
	
	//是否上架
	function shangjia()
    {
    	$data['is_on_sale'] = input("is_on_sale");
    	$res = db('goods')
    				->where('goods_id',input("id"))
    				->update($data);
        if($res){
			return json(['code'=>200,'msg'=>'修改成功']);
		}else{
			return json(['code'=>402,'msg'=>'修改失败']);
		}
    }
    
	//是否推荐
	function tuijian()
    {
    	$data['is_recommend'] = input("is_recommend");
    	$res = db('goods')->where('goods_id',input("id"))->update($data);
        if($res){
			return json(['code'=>200,'msg'=>'修改成功']);
		}else{
			return json(['code'=>402,'msg'=>'修改失败']);
		}
    }
    
    //删除商品
	function del_goods(){
		
		$res = db('goods')->where('goods_id',input('goods_id'))->update(['is_on_sale'=>3]);
		if($res){
			$this->success('删除成功', url('agent/shop/goods'));
		}else{
			$this->error('删除失败');
		}
		
	}
	
	//商品分类
	function types(){
		
		$types_list = db('good_cate')->where('agent_id',session('agent_id'))->order('id desc')->paginate(10);
    	$this->assign('types_list',$types_list);
		$this->assign('empty','<span class="empty" >没有数据</span>');
		return $this->fetch();
	}
	function types_add(){
		if(request()->isPost()){
			$data = $this->request->post();
			$data['agent_id'] = session('agent_id');
			$result = add_data('good_cate',$data);
			if($result){
				return json(['code'=>200,'msg'=>'添加成功']);
			}else{
				return json(['code'=>402,'msg'=>'添加失败']);
			}
		}
		return $this->fetch();
	}
	function types_edit(){
		
		
		if(request()->isPost()){
			$param=input('post.');
			$res = db('good_cate')->update($param);
			if($res){
				return json(['code'=>200,'msg'=>'修改成功']);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
		}
		
		
		$data = db('good_cate')->where('id',input('id'))->find();
		$this->assign('data',$data);
		return $this->fetch();
	}
	function types_del(){
		
		$res = db('good_cate')->where('id',input('id'))->delete();
		if($res){
			return json(['code'=>200,'msg'=>'删除成功']);
		}else{
			return json(['code'=>402,'msg'=>'删除失败']);
		}
	}
	
	//订单
	function orders($order_sn='',$order_name='',$status='',$begin_time='',$end_time=''){
		
		if(!empty($order_sn))$where['order_sn'] = ['like','%'.$order_sn.'%'];
		$this->assign('order_sn',$order_sn);
		if(!empty($status))$where['status'] = $status;
		$this->assign('status',$status);
		if(!empty($begin_time) && !empty($end_time)){
				$where['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		$where['agent_id']=session('agent_id');
		
		$list = db('good_order')
					->alias('a')
					->field('a.*,b.goods_name')
					->join('order_items b ','a.id = b.order_id')
					->where($where)
					->order('add_time desc')
					->paginate(4,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
		$this->assign('empty','<span class="empty" >没有数据</span>');
		return $this->fetch();
	}
	//订单发货
	function order_add($order_sn=''){
		$where['order_sn'] = $order_sn;
		$data = db('good_order')->where($where)->find();
    	$this->assign('data',$data);
		if(request()->isPost()){
			$param=input('post.');
			$param['status'] = 3;
			
			$res = db('good_order')->update($param);
			if($res){
				return json(['code'=>200,'msg'=>'发货成功']);
			}else{
				return json(['code'=>402,'msg'=>'发货失败']);
			}
		}
		return $this->fetch();
	}
	
	//评论
	function comment(){
		$list = db('good_comment a')->field('a.*,m.mobile')->join('member m', 'a.uid=m.user_id')->where('a.agent_id',session('agent_id'))->order('add_time desc')->paginate(5);
    	$this->assign('list',$list);
		$this->assign('empty','<span class="empty" >没有数据</span>');
		return $this->fetch();
	}
	public function address(){
		$where['shop_id']=session('agent_id');
		$res=db('address')->where('shop_id',session('agent_id'))->paginate(5);
		$this->assign('res',$res);
		return $this->fetch();
	}
	public function dz_add(){
            if(request()->isPost()){
			$data = $this->request->post();
			$result = add_data('address',$data);
			if ($result) {
				$this->success('添加成功');
			} else {
				$this->error('添加失败');
			}
		}
		 return $this->fetch();
	}
	function del(){
		$res = db('address')->where('id',input('id'))->delete();
		if($res){
			$this->success('删除成功', url('agent/shop/address'));
		}else{
			$this->error('删除失败');
		}
		
	}
	public function dz_edit(){
		if(request()->isPost()){
			$param=input('post.');
			$res = db('address')->where('id',input('id'))->update($param);
			if($res){
				$this->success('修改成功');
			}else{
				$this->success('修改失败');
			}
			
		}
		$res=db('address')->where('id',input('id'))->select();
		$this->assign('res',$res);
		return $this->fetch();
		
		
	}
	
	
}
